<?php

Raise::load('core.RaiseDataStream');

/**
 * TestCoreRaiseDataStream class
 * Testing unit of 'phpraise/core/RaiseDataStream.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseDataStream extends RaiseTest {

    public static function testStream(){
        self::assertClassExists('RaiseDataStream');
        self::assertClassExtends('RaiseDataStream', 'RaiseStream');

        $mockFile = new RaiseFile('testFile.raise');
        $mockFile->createFile();

        if($mockFile->exists()){
            $stream = new RaiseDataStream($mockFile->getStream());
            self::assertObjectClass($stream, 'RaiseDataStream');

            self::assertTrue($stream->isReadable());
            self::assertTrue($stream->isWriteable());
            self::assertTrue($stream->isSeekable());

            $str = 'hitesting';
            $stream->write($str);
            self::assertStrictEqual($stream->length(), strlen($str));
            self::assertStrictEqual($stream->position(), strlen($str));
            $stream->seek(2);
            self::assertStrictEqual($stream->position(), 2);
            self::assertStrictEqual($stream->read(4), substr($str, 2,4));
            $stream->seek(0);
            self::assertStrictEqual($stream->position(), 0);
            self::assertStrictEqual($stream->read(6), substr($str, 0,6));

            $stream->seek(6);
            $stream->write('eating');
            self::assertStrictEqual($stream->length(), 12);
            $stream->seek(0);
            self::assertStrictEqual($stream->read(12), 'hitesteating');

            self::assertStrictEqual($stream->position(), 12);

            $z = $stream->position();
            $stream->writeInteger(date('Y'));
            $stream->seek($z);
            self::assertStrictEqual($stream->readInteger(), (int)date('Y'));

            $z = $stream->position();
            $stream->writeBoolean(false);
            $stream->seek($z);
            self::assertFalse($stream->readBoolean());

            $z = $stream->position();
            $stream->writeBoolean(true);
            $stream->seek($z);
            self::assertTrue($stream->readBoolean());

            $z = $stream->position();
            $strValue = 'testing whether string works or not';
            $stream->writeString($strValue);
            $stream->seek($z);
            self::assertStrictEqual($stream->readString(), $strValue);

            $mockObj = new MockGenericObject();
            $mockObj->value = 'test';

            $z = $stream->position();
            $stream->writeObject($mockObj);
            $stream->seek($z);

            // two instance of object, thus cannot use Strict ===
            // instead we use == to check whether the value is the same
            self::assertEqual($stream->readObject(), $mockObj);

            self::assertStrictEqual($stream->length(), $stream->position());

            $stream->close();
            $mockFile->delete();
        }else{
            self::addError('Unable to create mock file');
        }
    }

}
